<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/20 上午11:42
 * +-------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\SysAuthMenu;
use app\admin\model\SysQuick;
use app\admin\model\Store;
use app\common\controller\AdminController;
use app\common\service\MenuService;
use think\facade\Cache;
use think\response\Json;

class Init extends AdminController
{

    protected array $userInfo;

    public function initialize(): void
    {
        parent::initialize();
        $this->userInfo = request()->adminUserInfo;
    }

    /**
     * 初始化信息
     * @return Json
     */
    public function init(): Json
    {
        //获取初始化信息缓存
        $cacheData = Cache::get('initAdmin_' . $this->adminUid);
        if (!empty($cacheData)) {
            return $this->successJson($cacheData);
        }
        //菜单服务类
        $menuService = new MenuService($this->adminUid);
        //管理员菜单id列表
        $auth_ids = explode(',',$this->userInfo['auth_ids']);
        $menu_ids = (new SysAuthMenu())->getAuthMenuList($auth_ids);
        $data        = [
                'logoInfo' => [
                        'title' => sysConfig('site', 'logo_title'),
                        'image' => sysConfig('site', 'logo_image'),
                        'href'  => __url('/index'), //默认地址
                ],
                'userInfo' => $this->userInfo,
                'menuInfo' => $menuService->getMenuTree($menu_ids),
        ];
        Cache::tag('initAdmin')->set('initAdmin_' . $this->adminUid, $data);
        return  $this->successJson($data);
    }

    /**
     * 初始化页面
     * @return Json
     */
    public function index(): Json
    {
        $storeModel = new Store();
        $storeNum = $storeModel->where('status',1)->count();
        $storeCityNum = $storeModel->where('type',1)->where('status',1)->count();
        $storeAreaNum = $storeModel->where('type',2)->where('status',1)->count();
        $storeWorkNum = $storeModel->where('type',3)->where('status',1)->count();

        $quickModel = new SysQuick();
        $quick      = $quickModel->where('status',1)->select()->toArray();

        $events     = null;
        return $this->successJson(compact('storeNum', 'storeCityNum', 'storeAreaNum', 'storeWorkNum', 'quick', 'events'));
    }
}